<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :inline="true" :model="searchObj" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.dishName" placeholder="菜品名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.category" placeholder="菜品类型"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.subCategory" placeholder="菜系"></el-input>
      </el-form-item>
      <el-select v-model="searchObj.dishStatus" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <!--点击查询按钮后提交到Onsubmit方法处理-->
      <el-button type="primary" @click="getByPage">查询</el-button>
      <el-button type="success" @click="reset">重置</el-button>
      <el-button type="danger" plain @click="deleteByIds">删除</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="菜品名称" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="img" label="菜品图片" width="150" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.dishPicture" style="width: 100px;height: 100px">
        </template>
      </el-table-column>
      <el-table-column label="菜品一级分类" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishClassification }}</span>
        </template>
      </el-table-column>
      <el-table-column label="菜品二级分类" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishSubclassification }}</span>
        </template>
      </el-table-column>
      <el-table-column label="菜品价格" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所需时间" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="150" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.dishStatus == '0' ? '已下架' : "在售" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="editStatus(scope.row.id)"
          >{{scope.row.dishStatus == '0' ? '上架' : '下架'}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页工具条 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 修改库存及价格的对话框 -->
    <el-dialog :visible.sync="dialogDishFormVisible" title="编辑菜品信息">
      <el-form :model="video" label-width="120px">
        <el-form-item label="价格">
          <el-input v-model="dish.dishPrice"/>
        </el-form-item>
        <el-form-item label="库存">
          <el-input v-model="dish.dishNumber"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDishFormVisible = false">取 消</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="editDish">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import dish from "@/api/dish";

export default {
  data() {
    return {
      multipleSelection: [], //存储被选中的每一个对象
      selectIds: "", //存储被选中的每一个对象的id
      listLoading: true, //是否显示loading信息
      list: [], //菜品数据列表
      page: 1, //页码
      limit: 5, //每页记录数
      total: 0, //总记录数
      currentPage: 1,
      searchObj: {},
      options: [
        {
          value: "1",
          label: "在售"
        },
        {
          value: "0",
          label: "停售"
        }
      ],
      dish: {},
      dialogDishFormVisible: false,
      saveBtnDisabled: false //保存按钮是否禁用
    };
  },
  mounted() {
    this.getByPage();
  },
  methods: {
    //保存修改
    editDish() {
      dish.editDish(this.dish).then(response => {
        this.$message({
          type: "success",
          message: "修改成功"
        });
        this.dialogDishFormVisible = false;
        this.getByPage();
      });
    },
    //打开修改菜品对话框
    handleEdit(row) {
      this.dish = {};
      dish.selectDish(row).then(response => {
        this.dish = response.data.dish;
        this.dialogDishFormVisible = true;
      });
    },
    //修改菜品状态
    editStatus(row) {
      dish.editStatus(row).then(response => {
        if (response.code == 20001) {
          this.$message({
            type: "error",
            message: response.data.message
          });
        } else {
          this.$message({
            type: "success",
            message: "修改状态成功"
          });
        }
        this.getByPage();
      });
    },
    reset() {
      this.searchObj = {};
    },
    handleSizeChange(val) {
      this.limit = val;
      this.getByPage();
    },
    handleCurrentChange(val) {
      (this.page = val), this.getByPage();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //带条件的分页
    getByPage() {
      dish
        .selectPageByCondition(this.page, this.limit, this.searchObj)
        .then(response => {
          this.list = response.data.pageInfo.records;
          this.total = response.data.pageInfo.total;
          this.listLoading = false;
        });
    },
    //批量删除菜品
    deleteByIds() {
      for (let i = 0; i < this.multipleSelection.length; i++) {
        let selectionElement = this.multipleSelection[i];
        if (i === this.selectIds.length - 1) {
          this.selectIds += selectionElement.id;
        } else {
          this.selectIds += selectionElement.id + ",";
        }
      }
      this.$confirm("此操作会永久删除数据，是否继续？", "确定删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          dish.deleteDish(this.selectIds).then(response => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getByPage();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
